package absolute_zero.com.urbox;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.ContactMethods;
import android.util.Log;

public class ContactAdder 
{
	public static void addContact(ContentResolver resolver, String filepath) //just pass getContentResolver() to it
	{
		BufferedReader reader;
		ArrayList<String> fileContents;
		ArrayList<String> photoContents;
		try
		{
			reader = new BufferedReader(new FileReader(filepath));
			fileContents = new ArrayList<String>();
			
			String line = reader.readLine();
			while (line != null)
			{
				fileContents.add(line);
				line = reader.readLine();
			}
			reader.close();
			
			photoContents = new ArrayList<String>();
			for (int index = fileContents.indexOf("Photo "); index < fileContents.size(); index ++)
			{
				photoContents.add(fileContents.get(index));
			}
			fileContents.removeAll(photoContents);
			photoContents.remove("Photo ");
			Log.d("New Contact Name", fileContents.get(0));
			
			ContentValues nameHolder = new ContentValues();
			ContentValues phoneValues, emailValues, addressValues, organizationValues, imValues;
			nameHolder.put(Contacts.People.NAME, fileContents.get(0));
			
			Uri newContact = Contacts.People.createPersonInMyContactsGroup(resolver, nameHolder);
			Uri phoneUri, emailUri, addressUri, organizationUri, imUri;
			

			

			if (newContact != null) //heaven forbid. i can't imagine what would go wrong for this to happen
			{
				phoneValues = new ContentValues();
				phoneUri = Uri.withAppendedPath(newContact, Contacts.People.Phones.CONTENT_DIRECTORY);
				emailValues = new ContentValues();
				emailUri = Uri.withAppendedPath(newContact, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
				addressValues = new ContentValues();
				addressUri = Uri.withAppendedPath(newContact, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
				organizationValues = new ContentValues();
				organizationUri = Uri.withAppendedPath(newContact, Contacts.Organizations.CONTENT_DIRECTORY);
				imValues = new ContentValues();
				imUri = Uri.withAppendedPath(newContact, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
				
				for (int index = 1; index < fileContents.size(); index ++)
				{
					line = fileContents.get(index);
					Log.d("Current Line Parsing", line);
					if (line.substring(0, line.indexOf(" ")).equals("Phone"))
					{
						if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Custom"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_CUSTOM);
							Log.d("New Custom Phone", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("FaxHome"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_FAX_HOME);
							Log.d("New FaxHome Phone", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("FaxWork"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_FAX_WORK);
							Log.d("New FaxWork Phone", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Home"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_HOME);
							Log.d("New Home Phone", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Mobile"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_MOBILE);
							Log.d("New Mobile Phone", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Other"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_OTHER);
							Log.d("New Other Phone", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Pager"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_PAGER);
							Log.d("New Pager Phone", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Work"))
						{
							phoneValues.put(Contacts.Phones.NUMBER, line.substring(line.indexOf(":") + 1));
							phoneValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_WORK);
							Log.d("New Work Phone", line.substring(line.indexOf(":") + 1));
						}
						resolver.insert(phoneUri, phoneValues);
					}
					else if (line.substring(0, line.indexOf(" ")).equals("Email"))
					{
						if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Custom"))
						{
							emailValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);
							emailValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_CUSTOM);
							emailValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Custom Email", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Home"))
						{
							emailValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);
							emailValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_HOME);
							emailValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Home Email", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Other"))
						{
							emailValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);
							emailValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_OTHER);
							emailValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Other Email", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Work"))
						{
							emailValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);
							emailValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_WORK);
							emailValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Work Email", line.substring(line.indexOf(":") + 1));
						}
						resolver.insert(emailUri, emailValues);
					}
					else if (line.substring(0, line.indexOf(" ")).equals("Address"))
					{
						if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Custom"))
						{
							addressValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL);
							addressValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_CUSTOM);
							addressValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Custom Address", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Home"))
						{
							addressValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL);
							addressValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_HOME);
							addressValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Home Address", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Other"))
						{
							addressValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL);
							addressValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_OTHER);
							addressValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Other Address", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Work"))
						{
							addressValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL);
							addressValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_WORK);
							addressValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Work Address", line.substring(line.indexOf(":") + 1));
						}
						resolver.insert(addressUri, addressValues);
					}
					else if (line.substring(0, line.indexOf(" ")).equals("Organization"))
					{
						if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Custom"))
						{
							organizationValues.put(Contacts.Organizations.COMPANY,line.substring(line.indexOf(":") + 1));
							organizationValues.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_CUSTOM);
							Log.d("New Custom Organization", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Other"))
						{
							organizationValues.put(Contacts.Organizations.COMPANY,line.substring(line.indexOf(":") + 1));
							organizationValues.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_OTHER);
							Log.d("New Other Organization", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Work"))
						{
							organizationValues.put(Contacts.Organizations.COMPANY,line.substring(line.indexOf(":") + 1));
							organizationValues.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);
							Log.d("New Work Organization", line.substring(line.indexOf(":") + 1));
						}
						resolver.insert(organizationUri, organizationValues);
					}
					else if (line.substring(0, line.indexOf(" ")).equals("IM"))
					{
						if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("AIM"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_AIM));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New AIM IM", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("GoogleTalk"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_GOOGLE_TALK));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Google Talk IM", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("ICQ"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_ICQ));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New ICQ IM", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Jabber"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_JABBER));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Jabber IM", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("MSN"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_MSN));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New MSN IM", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("QQ"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_QQ));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New QQ IM", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Skype"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_SKYPE));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Skype IM", line.substring(line.indexOf(":") + 1));
						}
						else if (line.substring(line.indexOf(" ") + 1, line.indexOf(":")).equals("Yahoo"))
						{
							imValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_IM);
							imValues.put(Contacts.ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(Contacts.ContactMethods.PROTOCOL_YAHOO));
							imValues.put(Contacts.ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
							imValues.put(Contacts.ContactMethods.DATA,line.substring(line.indexOf(":") + 1));
							Log.d("New Yahoo IM", line.substring(line.indexOf(":") + 1));
						}
						resolver.insert(imUri, imValues);
					}
				}
				if (!photoContents.contains("--No Photo--"))
				{
					byte[] photo = new byte[photoContents.size()];
					for (int index = 0; index < photoContents.size(); index ++)
					{
						photo[index] = Byte.parseByte(photoContents.get(index));
					}
					Contacts.People.setPhotoData(resolver, newContact, photo);
				}
				
			}
		}
		catch (FileNotFoundException e)
		{
			Log.e("Error finding file:", e.getMessage());
		}
		catch (IOException e)
		{
			Log.e("Error reading file:", e.getMessage());
		}

	}
		
}
